using Basis;
using UnityEngine;
namespace NL.Advanced
{


    public enum BulletFaceMode
    {
        none = 0, ForceTarget = 1, BySpeed = 2,

    }
    public class Bullet : TargetBB, IRudderOwner, _ISpeedable
    {
        float IRudderOwner.Life => _Lifing;
        float _ISpeedable.Speed { get => SpeedStart; set => SpeedStart = value; }

        Vector3 IRudderOwner.Speeding { get { return _Speeding; } set { _Speeding = value; } }

        Target IRudderOwner.Target => NowTarget;


        [Header("初始速度,加速度")]
        public float SpeedStart = 10f;


        public float Accelerate = 0;

        [Header("使用初始速度曲线")]
        public AnimationCurve AnimSpeedCurve;
        public float AnimSpeedLife;

        //[Header("跟踪目标得哪个悬挂点")]
        //public MountPos TargetMountPos = MountPos.Center;



        //[Header("跟踪释放半径.")]
        //public float TranckRelease = 1f;


        private Vector3 _LastPosition;


        private bool _LastHasTarget = false;


        [Header("---RUNTIME----即时速度")]
        public Vector3 _Speeding;





        protected override void Reset()
        {
            base.Reset();
        }

        protected override void Awake()
        {
            base.Awake();
            transform.localScale = Vector3.zero;
        }

        protected override void OnLive()
        {
            base.OnLive();




            _Rudder = GetComponent<Rudder>();



            if (NowTarget.Exist)
            {
                Vector3 _targetpos = NowTarget.SetMount("Center").Position;
                Vector3 _targetdir = _targetpos - transform.position;

                _Speeding = transform.forward * SpeedStart;
            }
            else
            {
                _Speeding = transform.forward * SpeedStart;
            }





            _LastHasTarget = NowTarget.Exist;
        }
        Rudder _Rudder;




        protected virtual Vector3 _CallulateSpeeding(Vector3 _targetpos)
        {
            if (_Rudder == null)
                return (_targetpos - transform.position).normalized * _Speeding.magnitude;
            else
                return _Speeding;
        }

        protected override void Update()
        {
            base.Update();

            if (!Living) return;


            //处理 发射启动阶段 速度处理.
            if (AnimSpeedLife > 0 && _Lifing < AnimSpeedLife)
            {
                float ds = 1 + AnimSpeedCurve.Evaluate(_Lifing / AnimSpeedLife);
                float _speed = ds * SpeedStart;
                _Speeding = _Speeding.normalized * _speed;
            }
            else
            {
                _Speeding += _Speeding.normalized * (Accelerate * Time.deltaTime);
            }




            if (!NowTarget.Exist)
            {

                transform.position += _Speeding * Time.deltaTime;

                _Dead(1f);

                return;
            }




            Vector3 _targetpos = NowTarget.SetMount("Center").Position;
            _LastPosition = _targetpos;

            if (AnimSpeedLife <= 0 || _Lifing > AnimSpeedLife)
                _Speeding = _CallulateSpeeding(_targetpos);// 

            Vector3 _dpos = _Speeding * Time.deltaTime;



            // 判断 到达..
            Vector3 _distance = _targetpos - transform.position;
            var to = transform.position + _dpos;
            var from = transform.position;
            var m = _targetpos;
            var a = from - to;
            var b = m - to;
            var angle = Vector3.Dot(a.normalized, b.normalized);

            if (angle >= 0)
            {

                _DoDead();
            }
            else
            {
                transform.position += _dpos;
            }
            _HandleDirection();

        }







    }

}

